package com.hcms.common.util;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {

    /**
     * 数据转换成树形结构
     *
     * @param list
     * @param rootId
     * @param <E>
     * @param <T>
     * @return
     */
    public static <E extends TreeNode<E, T>, T> List<E> builder(List<E> list, T rootId) {
        List<E> result = new ArrayList<>();
        list.forEach(itm -> {
            T id = itm.getId();
            T parentId = itm.getParentId();
            if (rootId.equals(parentId)) {
                // 获取children
                List<E> children = builder(list, id);
                itm.setChildren(children);
                result.add(itm);
            }
        });
        return result;
    }

}
